<template>
  <div class="loginView">
    <div class="blockDiv2">
      <img src="../../../public/img/bgImg/Snipaste_2020-05-14_11-51-03.png" />
      <div class="inner">
        <div class="innerText">
          <div class="title">已有账号？</div>
          <div class="subtitle">朋友，好久不见了！</div>
          <div class="zhuCeBtn" @click="goLogin">登录</div>
        </div>
      </div>
    </div>
    <div class="blockDiv">
      <img
        src="../../../public/img/logo/img1721517091251ef0.png"
        class="imgFloat"
      />
      <div class="loginLeft">
        <h1 class="titleH1">找回密码</h1>
        <el-form
          label-position="top"
          label-width="80px"
          :model="formLabelAlign"
          :rules="rules"
          ref="resgisterruleForm"
        >
          <!-- <el-form-item label="姓名" prop="name">
            <el-input v-model="formLabelAlign.name"></el-input>
          </el-form-item>-->
          <el-form-item label="手机号" prop="phone">
            <div style="display: flex">
              <el-input v-model="formLabelAlign.phone" type="number"></el-input>
              <el-button
                class="yanZhenButton"
                @click="sendCode"
                :disabled="disabledBtn"
                type="primary"
                >{{ codeTime }}</el-button
              >
            </div>
          </el-form-item>
          <el-form-item label="手机验证码" prop="code">
            <el-input v-model="formLabelAlign.code"></el-input>
          </el-form-item>
          <el-form-item label="请输入新密码" prop="password">
            <el-input
              v-model="formLabelAlign.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="请再次确认密码" prop="secondPassword">
            <el-input
              v-model="formLabelAlign.secondPassword"
              show-password
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="部门" prop="department">
            <el-input v-model="formLabelAlign.department"></el-input>
          </el-form-item>
          <el-form-item label="职位" prop="position">
            <el-input v-model="formLabelAlign.position"></el-input>
          </el-form-item>-->
          <!-- <div class="lostPassword">忘记密码</div> -->
          <el-form-item class="loginButtom">
            <el-button type="primary" @click="submitForm('resgisterruleForm')"
              >确认</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { forget_user_login } from "../../api/user_login";
import { PhoneMessage } from "../../api/sendmessage";
export default {
  data() {
    var checkNumber = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号码不能为空"));
      }
      setTimeout(() => {
        if (!/^1[345789]\d{9}$/.test(value)) {
          callback(new Error("请填写正确的手机号码格式"));
        } else {
          callback();
        }
      }, 1000);
    };
    var firstcheckPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空"));
      }
      setTimeout(() => {
        const reg = /^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)(?=.*?[!#@*&.$%^&*()_+-=></~`])[a-zA-Z\d!#@*&.$%^&*()_+-=></~`]{8,}$/;
        if (!reg.test(value)) {
          return callback(
            new Error("不能少于8位，且必须是大小写字母，数字和特殊字符的组合")
          );
        } else {
          callback();
        }
      }, 1000);
    };
    var checkPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空"));
      }
      setTimeout(() => {
        if (this.formLabelAlign.password) {
          if (
            this.formLabelAlign.password != this.formLabelAlign.secondPassword
          ) {
            callback(new Error("与初次输入密码不一致"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }, 1000);
    };
    return {
      timer: null,
      codeTime: "获取验证码",
      disabledBtn: false,
      sendPhoneCode: "",
      formLabelAlign: {
        phone: "",
        code: "",
        password: "",
        secondPassword: "",
      },
      rules: {
        phone: [{ validator: checkNumber, trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        password: [{ validator: firstcheckPassword, trigger: "blur" }],
        secondPassword: [{ validator: checkPassword, trigger: "blur" }],
      },
    };
  },
  destroyed: function () {
    // 每次离开当前界面时，清除定时器
    clearInterval(this.timer);
    this.timer = null;
    console.log("刷新触发");
  },
  methods: {
    setTimer() {
      this.$message({
        showClose: true,
        message: "验证码已发送",
        type: "success",
      });
      let time = 1000;
      let Time = 60;
      this.disabledBtn = true;
      if (this.timer == null) {
        this.timer = setInterval(() => {
          console.log("开始定时...每过一秒执行一次");
          Time = Time - 1;
          this.codeTime = Time + "s";
          if (Time == 0) {
            clearInterval(this.timer);
            this.timer = null;
            this.codeTime = "获取验证码";
            this.disabledBtn = false;
          }
        }, time);
      }
    },
    async sendCode() {
      clearInterval(this.timer);
      this.timer = null;
      this.setTimer();
      var data = {
        phone: this.formLabelAlign.phone,
      };
      var message = await PhoneMessage(data);
      console.log("短信", message);
      if (message.code == 200) {
        this.sendPhoneCode = message.data;
      } else {
        this.$message({
          message: message.data,
          type: "warning",
        });
      }
    },
    goLogin() {
      this.$router.push("/");
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          try {
            if (this.formLabelAlign.code != this.sendPhoneCode) {
              return this.$alert("手机验证码错误", "提示", {
                confirmButtonText: "确定",
                callback: () => {},
              });
            }

            let userData = {
              password: this.formLabelAlign.password,
              phone: this.formLabelAlign.phone,
            };
            let registrData = await forget_user_login(userData);
            console.log("注册回调", registrData);

            setTimeout(() => {
              loading.close();
              if (registrData.code == 200) {
                this.$alert("修改成功", "提示", {
                  confirmButtonText: "确定",
                  callback: () => {
                    this.$router.replace("/");
                  },
                });
              } else {
                this.$alert(registrData.message, "提示", {
                  confirmButtonText: "确定",
                  callback: () => {},
                });
              }
            }, 2000);
          } catch (error) {
            loading.close();
            this.$message.error("网络异常：" + error);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.loginView {
  margin-top: -30px;
  width: 100%;
  min-height: 650px;
  background: #fff;
  // text-align: center;
  display: flex;
  .blockDiv {
    width: 80%;
    // background: cornflowerblue;
    min-height: 650px;
    .imgFloat {
      float: right;
      width: 250px;
      margin: 10px;
    }
    .loginLeft {
      .el-form--label-top .el-form-item__label {
        padding: 0;
      }
      .el-form-item {
        margin-bottom: 5px;
      }
      .titleH1 {
        text-align: center;
      }
      width: 30%;
      margin: 0 auto;
      padding-top: 30px;
      .lostPassword {
        text-align: center;
        margin-top: 15px;
        font-size: 14px;
        text-decoration: underline;
        cursor: pointer;
        color: dimgray;
      }
      .loginButtom {
        text-align: center;
        .el-button {
          width: 100%;
          margin-top: 40px;
          background: #1d2f70;
          border: #1d2f70;
          color: #fff;
          margin-left: 5px;
          &:hover {
            background: #7d99e2;
          }
        }
      }
    }
  }
  .blockDiv2 {
    width: 20%;
    min-height: 650px;
    position: relative;
    img {
      // width: 100%;
      min-height: 650px;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      max-width: 100%;
      margin: auto;
      z-index: 1;
      opacity: 1;
    }
    .inner {
      color: #fff;
      position: absolute;
      z-index: 2;
      width: 100%;
      .innerText {
        width: 200px;
        text-align: center;
        margin: 0 auto;
        .title {
          margin-top: 90px;
          font-size: 28px;
          font-weight: 500;
          font-weight: 600;
        }
        .subtitle {
          margin-top: 10px;
          font-weight: 600;
        }
        .zhuCeBtn {
          border: 2px solid #fff;
          padding: 10px 10px;
          border-radius: 10px;
          font-size: 20px;
          font-weight: 600;
          margin-top: 220px;
          cursor: pointer;
        }
      }
    }
  }
}
.yanZhenButton {
  // width: 100%;
  // margin-top: 40px;
  // background: #1d2f70;
  // border: #1d2f70;
  // color: #fff;
  margin-left: 5px;
  // &:hover {
  //   background: #7d99e2;
  // }
}
</style>
